<template>
  <div id="app">
    <h1>食谱管理</h1>

    <!-- 添加新食谱的表单 -->
    <div class="add-recipe">
      <h2>添加新食谱</h2>
      <form @submit.prevent="addRecipe">
        <label for="name">食谱名称:</label>
        <input type="text" id="name" v-model="newRecipe.name" required>

        <label for="ingredients">食材:</label>
        <textarea id="ingredients" v-model="newRecipe.ingredients" required></textarea>

        <label for="instructions">步骤:</label>
        <textarea id="instructions" v-model="newRecipe.instructions" required></textarea>

        <button type="submit">添加</button>
      </form>
    </div>

    <!-- 食谱列表 -->
    <div class="recipe-list">
      <h2>食谱列表</h2>
      <ul>
        <li v-for="(recipe, index) in recipes" :key="index">
          <div class="recipe-item">
            <h3>{{ recipe.name }}</h3>
            <p><strong>食材:</strong> {{ recipe.ingredients }}</p>
            <p><strong>步骤:</strong> {{ recipe.instructions }}</p>
            <button @click="editRecipe(index)">编辑</button>
            <button @click="deleteRecipe(index)">删除</button>
          </div>
        </li>
      </ul>
    </div>

    <!-- 编辑食谱的模态框 -->
    <div v-if="editingRecipe !== null" class="edit-modal">
      <h2>编辑食谱</h2>
      <form @submit.prevent="saveEditedRecipe">
        <label for="edit-name">食谱名称:</label>
        <input type="text" id="edit-name" v-model="recipes[editingRecipe].name" required>

        <label for="edit-ingredients">食材:</label>
        <textarea id="edit-ingredients" v-model="recipes[editingRecipe].ingredients" required></textarea>

        <label for="edit-instructions">步骤:</label>
        <textarea id="edit-instructions" v-model="recipes[editingRecipe].instructions" required></textarea>

        <button type="submit">保存</button>
        <button @click="cancelEdit">取消</button>
      </form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      recipes: [],
      newRecipe: {
        name: '',
        ingredients: '',
        instructions: ''
      },
      editingRecipe: null
    };
  },
  methods: {
    addRecipe() {
      this.recipes.push({ ...this.newRecipe });
      this.newRecipe.name = '';
      this.newRecipe.ingredients = '';
      this.newRecipe.instructions = '';
    },
    editRecipe(index) {
      this.editingRecipe = index;
    },
    saveEditedRecipe() {
      this.editingRecipe = null;
    },
    cancelEdit() {
      this.editingRecipe = null;
    },
    deleteRecipe(index) {
      this.recipes.splice(index, 1);
    }
  }
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  margin: 20px;
}

.add-recipe, .recipe-list {
  margin-bottom: 20px;
}

.recipe-item {
  border: 1px solid #ccc;
  padding: 10px;
  margin-bottom: 10px;
}

.edit-modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: white;
  padding: 20px;
  border: 1px solid #ccc;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
</style>